package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
        private Connection con;
        private PreparedStatement pst;
        private ResultSet rs;

    @Override
        public List<Cart> listCart(Cart cart) throws Exception {
        List<Cart> list =new ArrayList<>();
        Food food = null;
        Business business = null;
        String sql="SELECT cartId,c.foodId,c.businessId,userId,quantity,f.foodId,foodName,foodExplain,foodImg,foodPrice,f.businessId,f.remarks,b.businessId,businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,b.remarks FROM cart c,food f,business b WHERE c.foodId=f.foodId AND c.businessId=b.businessId AND userId=?";
        StringBuffer stringBuffer = new StringBuffer(sql);
        try {
            if (cart.getBusinessId() == null){
                con = DBUtil.getConnection();
                pst = con.prepareStatement(sql);
               pst.setString(1,cart.getUserId());

            }else {
                //Connection从ThreadLocal中获取
                con = DBUtil.getConnection();
                stringBuffer.append("and c.businessId=?");
                pst = con.prepareStatement(String.valueOf(stringBuffer));
                pst.setString(1,cart.getUserId());
                pst.setInt(2,cart.getBusinessId());
            }
            //查询
            rs = pst.executeQuery();
            while (rs.next()){
                cart =new Cart();
                cart.setCartId(rs.getInt("cartId"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                cart.setUserId(rs.getString("userId"));
                cart.setQuantity(rs.getInt("quantity"));
                food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                cart.setFood(food);
                cart.setBusiness(business);
                list.add(cart);
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return list;
    }

        @Override
        public int saveCart(String userId, Integer businessId, Integer foodId) throws  Exception{
            String sql = "insert into cart (userId,businessId,foodId,quantity) value(?,?,?,1)";
            int add=0;
            try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            //将？替换具体的值
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setInt(3,foodId);
                //执行查询
               add = pst.executeUpdate();
            }finally {
                DBUtil.close(rs,pst);
            }
            return add;
        }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) throws Exception {
        Integer update=0;
        String sql="INSERT INTO cart (userId,businessId,foodId,quantity) VALUES(?,?,?,1)";
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            //将?替换具体的值
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setInt(3,foodId);
            //调用方法
            update= pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return update;
    }

    @Override
    public Integer removeCart(Cart cart) throws Exception {
        Integer del= 0;
        try {
            String sql = "DELETE FROM cart WHERE userId=? AND businessId=?";
            StringBuffer stringBuffer = new StringBuffer(sql);
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            if (cart.getFoodId()==null) {
                pst = con.prepareStatement(sql);
                //将?替换具体的值
                pst.setString(1, cart.getUserId());
                pst.setInt(2, cart.getBusinessId());
            } else {
                stringBuffer.append(" AND foodId=?");
                pst = con.prepareStatement(String.valueOf(stringBuffer));
                pst.setString(1, cart.getUserId());
                pst.setInt(2, cart.getBusinessId());
                pst.setInt(3, cart.getFoodId());
            }
            //调用删除方法
            del= pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }

        return del;
    }
}
